﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CRegex
{
   public class C_Regex
    {

        /// <summary>
        /// 是否为日期型字符串
        /// </summary>
        /// <param name="StrSource">日期字符串(2008-05-08)</param>
        /// <returns></returns>
        public bool IsDate(string StrSource)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
        }
        /// <summary>
        /// 是否为时间型字符串
        /// </summary>
        /// <param name="source">时间字符串(15:00:00)</param>
        /// <returns></returns>
        public bool IsTime(string StrSource)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }
        /// <summary>
        /// 是否为整数
        /// </summary>
        /// <param name="source">0-9</param>
        /// <returns></returns>
        public bool IsInt(string StrSource)//
        {
            return System.Text.RegularExpressions.Regex.IsMatch(StrSource, @"^[0-9]\d*$");
        }
       /// <summary>
        /// 是否为身份证号
        /// </summary>
        /// <param name="source">0-9</param>
        /// <returns></returns>
        public bool IsIdentity(string StrSource)//
        {
            return System.Text.RegularExpressions.Regex.IsMatch(StrSource, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
        }

        /// <summary>
        /// 带小数点数字匹配
        /// </summary>
        /// <param name="StrSource"></param>
        /// <returns></returns>
        public bool IsNumber(string StrSource)
        {
            string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
            return System.Text.RegularExpressions.Regex.IsMatch(StrSource, pattern);
        }
      
        /// <summary>
        /// 是否为IP地址
        /// </summary>
        /// <param name="source">0-9</param>
        /// <returns></returns>
        public static bool IsIP(string StrSource)
        {

            return System.Text.RegularExpressions.Regex.IsMatch(StrSource, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
        /// <summary>
        /// 串口设置检验格式
        /// </summary>
        /// <param name="sdata"></param>
        /// <returns></returns>
        public static bool IsCOM(string sdata)
        {
          //  bool b = false;
            string tiaojian = @"^(COM\d{1,2}):([0-9]*):(\d):(\d):(\d)$";
           // Regex ret = new Regex(tiaojian);
            return System.Text.RegularExpressions.Regex.IsMatch(sdata, tiaojian);
            //return b;
        }

        public static bool checkcom(string scom, bool zt)
        {
            if (zt)
            {
                if (!CRegex.C_Regex.IsCOM(scom))
                {
                    DialogResult result = MessageBox.Show(string.Format(@"当前信号处理板形式为：{0} " + "\n\r" + "正确串口格式为:COM(X:数字):19200(波特率）:0:0:1 " + "\n\r" +
              "实例:COMx:4800:8:0:1", scom), "提示", MessageBoxButtons.OKCancel);
                    if (result == DialogResult.OK)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }
    }
}
